package com.example.mall.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.util.Assert;

import java.util.List;

@AllArgsConstructor
@Getter
public enum SymbolEnum {

    COMMA_ZH("，", "逗号（中文）"),
    COMMA_EN(",", "逗号（英文）"),
    DOT(".", "点"),
    PERIOD("。", "句号"),
    BAR("-", "横杠"),
    UNDERLINE("_", "下划线"),
    SLASH("/", "正斜杠"),
    BACKSLASH("\\", "反斜杠"),
    EMPTY("", "空字符串"),
    SPACE(" ", "空格"),
    ;

    /** 符号编码 */
    private final String code;

    /** 符号名称 */
    private final String name;

    public static String concatWithSlash(List<String> resources) {
        Assert.notEmpty(resources, "集合不能为空");
        StringBuilder stringBuilder = new StringBuilder(SLASH.code);
        resources.stream().filter(StringUtils::isNotEmpty).forEach(resource -> stringBuilder.append(getResourceWithoutSlash(resource)).append(SLASH.code));
        return DriveLetterEnum.getStartsWithDriveLetterIfExists(stringBuilder.toString());
    }

    @NotNull
    private static String getResourceWithoutSlash(String resource) {
        String resourceWithoutSlash = resource;
        if (resource.startsWith(SLASH.code)) {
            resourceWithoutSlash = resourceWithoutSlash.substring(SLASH.code.length());
        }
        if (resource.endsWith(SLASH.code)) {
            resourceWithoutSlash = resourceWithoutSlash.substring(0, resourceWithoutSlash.length() - SLASH.code.length());
        }
        return resourceWithoutSlash;
    }

}
